package logic

import (
	"context"
	"fmt"
	"tieba/models/mysql"

	"tieba/tiebarpc/internal/svc"
	"tieba/tiebarpc/tiebarpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type LikeAddLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLikeAddLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LikeAddLogic {
	return &LikeAddLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *LikeAddLogic) LikeAdd(in *tiebarpc.LikeAddRequest) (*tiebarpc.LikeAddResponse, error) {
	// todo: add your logic here and delete this line
	var tz mysql.TieZi
	mysql.DB.Debug().Where("id = ?", in.TieZiId).Find(&tz)
	tz.Likes = tz.Likes + 1
	mysql.DB.Debug().Updates(&tz)
	var like mysql.Like
	mysql.DB.Debug().Where("user_id = ?", in.UserId).Where("tie_zi_id = ?", in.TieZiId).Find(&like)
	if like.ID != 0 {
		return nil, fmt.Errorf("当前用户已点赞过这个帖子了")
	}
	like.UserId = int(in.UserId)
	like.TieZiId = int(in.TieZiId)
	mysql.DB.Debug().Create(&like)
	return &tiebarpc.LikeAddResponse{LikeId: int64(like.ID)}, nil
}
